package club.haomoon.boss.service.impl.admin;

import club.haomoon.boss.service.admin.IAdminRoleService;
import club.haomoon.core.entity.admin.AdminRoleData;
import club.haomoon.core.mapper.admin.AdminRoleDataMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import java.util.*;

/**
 * -------------------------
 * @Description: 系统角色服务实现类
 * @Author: Wangjianwei
 * @Date: 2025/02/17 17:14
 * -------------------------
 */
@Service
public class AdminRoleServiceImpl implements IAdminRoleService {

    @Resource
    AdminRoleDataMapper adminRoleDataMapper;
    @Override
    public List<Integer> getRoleIdAttr(Integer adminId) {
        List<Integer> ret = new ArrayList<>();
        List<AdminRoleData> rolesList = adminRoleDataMapper.selectList(new QueryWrapper<AdminRoleData>().eq("admin_id", adminId).select("role_id"));
        if (rolesList.size() > 0) {
            for (AdminRoleData adminRole : rolesList) {
                ret.add(adminRole.getRoleId());
            }
        }
        return ret;
    }

    @Override
    public List<AdminRoleData> getAdminIdByRoleId(Integer roleId) {
        List<AdminRoleData> result = adminRoleDataMapper.selectList(new QueryWrapper<AdminRoleData>().eq("role_id", roleId).select("admin_id"));
        return result;
    }

    @Override
    public void batchInsert(Integer adminId, List<Integer> roleIds) {
        this.deleteByAdminId(adminId);
        if (roleIds != null && !roleIds.isEmpty()) {
            roleIds.forEach(item-> {
                this.adminRoleDataMapper.insert(new AdminRoleData(){{
                    setAdminId(adminId);
                    setRoleId(item);
                }});
            });
        }
    }

    @Override
    public void deleteByAdminId(Integer adminId) {
        this.adminRoleDataMapper.delete(new QueryWrapper<AdminRoleData>().eq("admin_id", adminId));
    }
}
